package com.itheima.upload3;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/*
上传图片的 客户端
1.读本地图片
2.向服务器上传图片
3.读取服务器的上传成功
 */
public class Client {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 8181);
        OutputStream os = socket.getOutputStream();
        //使用缓冲流  包装 普通流  提升 效率
        BufferedOutputStream bos = new BufferedOutputStream(os);
        //1.读本地图片
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\美女\\meinv2.jpg"));
        byte[] bytes = new byte[1024];
        //2.向服务器上传图片
        int len;
        while ((len=bis.read(bytes))!=-1){
            //bytes 0, len
            bos.write(bytes,0,len);
        }
        bos.flush();
        socket.shutdownOutput();


        //3.读取服务器的上传成功
        //字节
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String s = br.readLine();
        System.out.println(s);

        //释放资源
        bis.close();
        socket.close();

    }
}
